const { default: axios } = require('axios');
const { getSqlLiteData } = require('../../SqlLiteDB');

module.exports = function setWorkProcedureRouter (router) {

    //查询工序
    router.get('/getWorkProcedures', async function (req, res, next) {
        try {
            let dataContent = JSON.parse(req.query['dataContent']);
            let active = dataContent.active;
            let SqlString = "SELECT * FROM WORKPROCEDURE U" + (active ? ` WHERE U.is_active='${active}'` : '');
            let procedures = await getSqlLiteData(SqlString);

            res.status(200).json({
                procedures,
            });
        }
        catch (ex) {
            res.status(500).json({
                result: ex,
            });
            console.log('ex :>> ', {
                result: ex,
            });
        }
    });

    //新增或修改工序
    router.post('/updateWorkProcedure', async function (req, res, next) {
        try {
            let dataContent = JSON.parse(req.body['dataContent']);
            let procedure = dataContent.procedure;
            let SqlString = `REPLACE INTO WORKPROCEDURE(worprocedureid,wpid,wpname,wpworktime,wpworktime_sd,wpworktime_sb,is_active) 
            VALUES ('${procedure.worprocedureid}', '${procedure.wpid}', '${procedure.wpname}',
                 '${procedure.wpworktime}','${procedure.wpworktime_sd}','${procedure.wpworktime_sb}',
                 ${procedure.is_active})`;
            let result = await getSqlLiteData(SqlString);

            res.status(200).json({
                result,
            });
        }
        catch (ex) {
            res.status(500).json({
                result: ex,
            });
            console.log('ex :>> ', {
                result: ex,
            });
        }
    });

    //删除工序
    router.post('/deleteWorkProcedure', async function (req, res, next) {
        try {
            let dataContent = JSON.parse(req.body['dataContent']);
            let worprocedureid = dataContent.worprocedureid;

            let matSqlString = `SELECT * FROM matworkprocedure m WHERE worprocedureid ='${worprocedureid}' ORDER BY m.matcode,m.orderindex DESC`;
            let matResult = await getSqlLiteData(matSqlString);
            let matResultObj = {};
            for (let i = 0; i < matResult.length; i++) {
                const row = matResult[i];
                let paramData = {
                    "procedure":
                    {
                        "matcode": row.matcode,
                        "orderindex": row.orderindex,
                        "worprocedureid": row.worprocedureid,
                    }
                };
                let axiosResponse = await axios.post('http://127.0.0.1:14603/supplier/deleteMatWorkProcedure', {
                    dataContent: JSON.stringify(paramData),
                });
                matResultObj[row.matcode + ':' + row.worprocedureid + ':' + row.orderindex] = axiosResponse.data.result;
                matResultObj[row.matcode + ':' + row.worprocedureid + ':' + row.orderindex + ':1'] = axiosResponse.data.resultIndex1;
                matResultObj[row.matcode + ':' + row.worprocedureid + ':' + row.orderindex + ':2'] = axiosResponse.data.resultIndex2;
            }

            let SqlString = `DELETE FROM WORKPROCEDURE WHERE worprocedureid ='${worprocedureid}'`;
            let result = await getSqlLiteData(SqlString);

            res.status(200).json({
                result,
                ...matResultObj,
            });
        }
        catch (ex) {
            res.status(500).json({
                result: ex,
            });
            console.log('ex :>> ', {
                result: ex,
            });
        }
    });
};
